--Patch fix start
--[[local function XXXPatchFix()
	if (IsAddOnLoaded("XXX")) then
		butt:SetNormalTexture("Interface\\Addons\\XXX\\YYY\\ZZZ\\clothed")
		butt:SetPushedTexture("Interface\\Addons\\XXX\\YYY\\ZZZ\\nude")
	elseif (IsAddOnLoaded("YYY")) then
		butt:SetNormalTexture("Interface\\Addons\\YYY\\ZZZ\\clothed")
		butt:SetPushedTexture("Interface\\Addons\\YYY\\ZZZ\\nude")
	end
end
	if (IsAddOnLoaded("XXX")) then
	elseif (IsAddOnLoaded("YYY")) then
	end

]]--Patch fix end

----------
--Query

local _G = _G

-- LOCAL VARS--
local tonumber = _G.tonumber
local GetItemInfo = _G.GetItemInfo
local GetSpellInfo = _G.GetSpellInfo


-- GENERIC PRINT --
local function printitem(message,frame)
	frame = frame or _G.DEFAULT_CHAT_FRAME
	frame:AddMessage(message)
end

-- SLASH QUERYING --
_G.SLASH_ITEMQUERY1 = "/item";
_G.SLASH_ITEMQUERY2 = "/wupin";
_G.SLASH_SPELLQUERY1 = "/jineng";
_G.SLASH_QUESTQUERY1 = "/quest";
_G.SLASH_QUESTQUERY2 = "/renwu";
_G.SLASH_ENCHANTQUERY1 = "/enchant";
_G.SLASH_ENCHANTQUERY2 = "/shangye";

function _G.SlashCmdList.ITEMQUERY(ID)
	if tonumber(ID) then
		local _,link = GetItemInfo(ID)
		if link then
			printitem("|cffFFFF7F[\231\137\169\229\147\129|r|cffFF7F7F\230\159\165\232\175\162|r|cffFFFF7F]|r ItemID \"|cff9999FF"..ID.."|r\" \229\156\168\231\188\147\229\173\152\228\184\173\230\137\190\229\136\176: "..link..".")
		else
			printitem("|cffFFFF7F[\231\137\169\229\147\129|r|cffFF7F7F\230\159\165\232\175\162|r|cffFFFF7F]|r ItemID \"|cff9999FF"..ID.."|r\" \230\156\170\229\156\168\231\188\147\229\173\152\228\184\173\230\137\190\229\136\176. |cffFF0000|Hitem:"..ID..":0:0:0:0:0:0:0|h[\231\130\185\229\135\187\230\159\165\232\175\162\231\137\169\229\147\129]|r.")
		end
	else
		printitem("|cffFFFF7F[\231\137\169\229\147\129|r|cffFF7F7F\230\159\165\232\175\162|r|cffFFFF7F]|r |cffFF3333\233\148\153\232\175\175:|r \230\151\160\230\149\136\231\154\132\232\190\147\229\133\165; ItemID\229\191\133\233\161\187\230\152\175\228\184\128\228\184\170\230\149\176\229\128\188.")
	end
end
function _G.SlashCmdList.SPELLQUERY(ID)
	if tonumber(ID) then
	local spellName = GetSpellInfo(ID)
		if spellName then
			printitem("|cffFFFF7F[\230\138\128\232\131\189|r|cffFF7F7F\230\159\165\232\175\162|r|cffFFFF7F]|r SpellID \"|cff9999FF"..ID.."|r\" \229\183\178\230\159\165\232\175\162\229\136\176: ".."|cff71d5ff|Hspell:"..ID.."|h["..spellName.."]|r.")
		else
			printitem("|cffFFFF7F[\230\138\128\232\131\189|r|cffFF7F7F\230\159\165\232\175\162|r|cffFFFF7F]|r SpellID \"|cff9999FF"..ID.."|r \228\184\141\230\152\175\228\184\128\228\184\170\230\156\137\230\149\136\230\138\128\232\131\189")
		end
	else
		printitem("|cffFFFF7F[\230\138\128\232\131\189|r|cffFF7F7F\230\159\165\232\175\162|r|cffFFFF7F]|r |cffFF3333\233\148\153\232\175\175:|r \230\151\160\230\149\136\231\154\132\232\190\147\229\133\165; SpellID\229\191\133\233\161\187\230\152\175\228\184\128\228\184\170\230\149\176\229\128\188.")
	end
end
function _G.SlashCmdList.QUESTQUERY(ID)
	local tip = CreateFrame("GameTooltip", "JYTooltip", UIParent, "GameTooltipTemplate");
	tip:ClearLines();
	tip:SetOwner(UIParent, "ANCHOR_NONE");
	tip:SetHyperlink("quest:"..ID);
	local questName;
	if tip:NumLines() > 0 then questName = JYTooltipTextLeft1:GetText() end
	--if questName then ChatFrame1:AddMessage(questName) end
	if tonumber(ID) then
		if questName then
			printitem("|cffFFFF7F[\228\187\187\229\138\161|r|cffFF7F7F\230\159\165\232\175\162|r|cffFFFF7F]|r QuestID \"|cff9999FF"..ID.."|r\" \229\183\178\230\159\165\232\175\162\229\136\176: ".."|cffffff00|Hquest:"..ID.."|h["..questName.."]|r.")
		else
			printitem("|cffFFFF7F[\228\187\187\229\138\161|r|cffFF7F7F\230\159\165\232\175\162|r|cffFFFF7F]|r QuestID \"|cff9999FF"..ID.."|r \230\156\170\229\156\168\231\188\147\229\173\152\228\184\173\230\137\190\229\136\176. |cffff0000|Hquest:"..ID.."|h[\231\130\185\229\135\187\230\159\165\232\175\162\228\187\187\229\138\161]|r.")
		end
	else
		printitem("|cffFFFF7F[\228\187\187\229\138\161|r|cffFF7F7F\230\159\165\232\175\162|r|cffFFFF7F]|r |cffFF3333\233\148\153\232\175\175:|r \230\151\160\230\149\136\231\154\132\232\190\147\229\133\165; QuestID\229\191\133\233\161\187\230\152\175\228\184\128\228\184\170\230\149\176\229\128\188.")
	end
end
function _G.SlashCmdList.ENCHANTQUERY(ID)
	local tip = CreateFrame("GameTooltip", "JYTooltip", UIParent, "GameTooltipTemplate");
	tip:ClearLines();
	tip:SetOwner(UIParent, "ANCHOR_NONE");
	tip:SetHyperlink("enchant:"..ID);
	local enchantName;
	if tip:NumLines() > 0 then enchantName = JYTooltipTextLeft1:GetText() end
	if tonumber(ID) then
		if enchantName then
			printitem("|cffFFFF7F[\229\149\134\228\184\154|r|cffFF7F7F\230\159\165\232\175\162|r|cffFFFF7F]|r EnchantID \"|cff9999FF"..ID.."|r\" \229\183\178\230\159\165\232\175\162\229\136\176: ".."|cff71d5ff|Henchant:"..ID.."|h["..enchantName.."]|r.")
		else
			printitem("|cffFFFF7F[\229\149\134\228\184\154|r|cffFF7F7F\230\159\165\232\175\162|r|cffFFFF7F]|r EnchantID \"|cff9999FF"..ID.."|r \228\184\141\230\152\175\228\184\128\228\184\170\230\156\137\230\149\136\229\149\134\228\184\154\230\138\128\232\131\189")
		end
	else
		printitem("|cffFFFF7F[\229\149\134\228\184\154|r|cffFF7F7F\230\159\165\232\175\162|r|cffFFFF7F]|r |cffFF3333\233\148\153\232\175\175:|r \230\151\160\230\149\136\231\154\132\232\190\147\229\133\165; EnchantID\229\191\133\233\161\187\230\152\175\228\184\128\228\184\170\230\149\176\229\128\188.")
	end
end

---------
--[[便捷使用种族语言]]

local l_SECOND_LANGUAGE;


SlashCmdList["SlashLanguage"] = function(msg)
	if (l_SECOND_LANGUAGE == nil) then
		l_SECOND_LANGUAGE = (GetLanguageByIndex(2) or GetLanguageByIndex(1));
	end

	local chatType, realMsg = msg:match("^%[([^%]]+)%] (.+)");
	local target;
	if (chatType ~= nil) then
		msg = realMsg;
		chatType = chatType:lower();
		local realChatType, potentialTarget = chatType:match("^(%S+) (.+)");
		if (realChatType ~= nil) then
			chatType = realChatType;
			target = (GetChannelName(potentialTarget) or potentialTarget);
		end
	else
		local editBox = this;
		chatType = (editBox:GetAttribute("chatType") or editBox.chatType);
		target = (editBox:GetAttribute("tellTarget") or editBox.tellTarget);
	end

	SendChatMessage(msg, chatType, l_SECOND_LANGUAGE, target);
end

SLASH_SlashLanguage1 = ("/lan");


----------
--[[!EVA_TxtMessage 提示消息]]
--创建并初始化插件
--[[
local EVA_TextMessage = EVA.Addon:New()

--设置插件相关描述
EVA_TextMessage.Name ="Commander Text"
EVA_TextMessage.Version ="0.1"
EVA_TextMessage.Description ="使用 /tip 显示文本. By 昏睡墨鱼. URL[http://wiki.cwowaddon.com/]"

--将插件添加到插件注册表中
EVA.AddonRegistry:Add(EVA_TextMessage)

function PrintTextMessage(msg)
	local text = msg.Slash_Arg
	--添加颜色
	text = "|cff7fff7f".. text .."|r";
	--显示文本
	DEFAULT_CHAT_FRAME:AddMessage(text);
end

	--创建功能集
local TextMessage = {
	RequestTypes = {"SlashCommand"},
	SlashCommands = {"/tip"},
	Commands = {
		{Function = PrintTextMessage},
	}
}

--将功能集合添加到插件中
EVA_TextMessage:Add(EVA.Block:New(TextMessage))
]]

----------
--[[库存查询]]
local origOnTooltipSetItem = GameTooltip:GetScript("OnTooltipSetItem")
GameTooltip:SetScript("OnTooltipSetItem", function(...)
	local item = select(2, GameTooltip:GetItem())
	if item then
		local bags, all = GetItemCount(item), GetItemCount(item, true)
		if all > 0 then
			GameTooltip:AddDoubleLine("\229\133\177\230\156\137 "..all, "(\232\131\140\229\140\133: "..bags.." / \233\147\182\232\161\140: "..(all-bags)..")")
		end
	end
	return origOnTooltipSetItem(GameTooltip, ...)
end)


----------
--[[快速变焦 Fastzoom

local printstack = -1
function Fastzoomprint(message)
   if(Fastzoomdebug) then
      local stack, filler
      if not message then
	  	--DEFAULT_CHAT_FRAME:AddMessage("No Message to print.")
		 return false
      end
      printstack=printstack+1
      local filler=""
      for stack=1,printstack do
	  	 filler=filler.."--"
      end
      if (type(message) == "table") then
	  	-- DEFAULT_CHAT_FRAME:AddMessage("its a table.  length="..SL_tcount(message))
	 	local i
	 	for k,v in pairs(message) do

	    	DEFAULT_CHAT_FRAME:AddMessage(filler.."(key)   "..k)
	    	Fastzoomprint(v)
	 	end
      elseif (type(message) == "userdata") then

      else
	  	  if(printstack>0) then
	    	 DEFAULT_CHAT_FRAME:AddMessage(filler.."(value) "..message)
	 	 else
	    	 DEFAULT_CHAT_FRAME:AddMessage(filler..message)
	 	 end
	end
      printstack=printstack-1
   end
end

function Fastzoominitialize()

   --Fastzoomprint("|c00aaffffvariables loaded.  Initializing.")

   local playerName = UnitName("player");
   local serverName = GetCVar("realmName");
   if (playerName == nil or playerName == UNKNOWNOBJECT or playerName == UNKNOWNBEING) then
      return;
   end

   if (FASTZOOMSPEED) then                     --old version

   else
        Fastzoomprint("设置默认值!")
      --set defaults here
        FASTZOOMSPEED = 5
        SetBinding("CTRL-MOUSEWHEELUP","FastZoomIn")
        SetBinding("CTRL-MOUSEWHEELDOWN","FastZoomOut")
        SaveBindings(2)
   end

end

-- ONLOAD, duh
function Fastzoom_OnLoad()

   -- Register for events
   --  this:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
   Fastzoomframe = CreateFrame("Frame")
   Fastzoomframe:RegisterEvent("VARIABLES_LOADED")
   Fastzoomframe:RegisterEvent("UPDATE_BINDINGS")

   Fastzoomframe:SetScript("OnEvent",Fastzoom_OnEvent)

   --DEFAULT_CHAT_FRAME:AddMessage("FastZoom Loaded.  Type '/Fastzoom X' to change speed")

   -- create our Fastzoomash commands
   SLASH_Fastzoom1 = "/Fastzoom";
   SLASH_Fastzoom2 = "/fastzoom";
   SLASH_Fastzoom3 = "/FZ";
   SLASH_Fastzoom4 = "/fz";

   SlashCmdList["Fastzoom"] = Fastzoommain;

  	BINDING_HEADER_FastZoom = "快速变焦"
	BINDING_NAME_FastZoomIn = "快速放大"
	BINDING_NAME_FastZoomOut = "快速缩小"

   --Fastzoomprint("|c00449955 end onload ")

end
---------------------------------------------------------------------------

function FastZoomIn()
   CameraZoomIn(FASTZOOMSPEED)
end

function FastZoomOut()
   CameraZoomOut(FASTZOOMSPEED)
end

function Fastzoommain(input)

   --Fastzoomprint("someone did a /Fastzoom")

   local binding = GetBindingKey("FastZoomIn")
   if(binding) then
        Fastzoomprint("getkeybinding = "..binding        )
    end
   if not (tonumber(input)) then
        DEFAULT_CHAT_FRAME:AddMessage("键入 /Fastzoom '数字(范围1-50)'")
   else
        DEFAULT_CHAT_FRAME:AddMessage("变焦速度设置为 "..tonumber(input))
        FASTZOOMSPEED = tonumber(input)
   end

end

function Fastzoom_OnEvent()
   -- Fastzoomprint("|c00aaffaa  event Triggered!  ")
    Fastzoomprint(event)
    this:UnregisterEvent(event)
   --local timestaFastzoom, event, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags = CombatLogGetCurrentEntry()  --for documentation purposes
   --if (event == "VARIABLES_LOADED") then
   if (event == "UPDATE_BINDINGS") then

      Fastzoominitialize()

   end
end

Fastzoom_OnLoad()]]

----------
--[[AFK 自动旋转镜头
function AfkSpin_OnEvent()
	if event=="ADDON_LOADED" then
		SetCVar("cameraYawMoveSpeed","230")
	elseif event=="CHAT_MSG_SYSTEM" then
		if (arg1 == format(MARKED_AFK_MESSAGE,DEFAULT_AFK_MESSAGE) and GetCVar("autoClearAFK") == "1") then
			SetView(5)
			SetCVar("cameraDistanceMax",50)
			SetCVar("cameraYawMoveSpeed","8")
			MoveViewRightStart()
		elseif arg1 == CLEARED_AFK then
			MoveViewRightStop()
			SetCVar("cameraYawMoveSpeed","230")
			SetCVar("cameraDistanceMax",GetCVar("cameraDistanceMax"))
			SetView(5)
		end
	end
end

local frame = CreateFrame("Frame", "ASFrame");
frame:SetScript("OnEvent", AfkSpin_OnEvent);
frame:RegisterEvent("ADDON_LOADED");
frame:RegisterEvent("CHAT_MSG_SYSTEM");]]

----------
--[[或者增益感谢消息]]
local t4bf = CreateFrame('Frame');
t4bf:SetScript('OnEvent', function(self, event, ...)
        --DEFAULT_CHAT_FRAME:AddMessage("Working # 1")
		local timestamp, eventType, sourceGUID, sourceName, sourceFlags, destGUID, destName, destFlags = select(1, ...)
		local filter = bit.bor(COMBATLOG_OBJECT_AFFILIATION_OUTSIDER, COMBATLOG_OBJECT_REACTION_FRIENDLY, COMBATLOG_OBJECT_CONTROL_MASK, COMBATLOG_OBJECT_TYPE_MASK)
		if eventType == "SPELL_AURA_APPLIED" and destGUID == UnitGUID("player") and (CombatLog_Object_IsA(sourceFlags, filter) == 1) then
			local spellId, spellName, spellSchool = select(9, ...)
			if (not InCombatLockdown())  then
				if( GetLocale() == "zhCN" ) then
					SendChatMessage("感谢 "..sourceName.." 送予的 "..spellName..".", "EMOTE")
				elseif( GetLocale() == "zhTW" ) then
					SendChatMessage("感謝 "..sourceName.." 送予的 "..spellName..".", "EMOTE")
				else
					SendChatMessage("thanks "..sourceName.." for their "..spellName..".", "EMOTE")
				end
			end
        end
    end);
t4bf:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")

----------
--[[鼠标边框品质着色

local select = select
local GetItemInfo = GetItemInfo
local GetItemQualityColor = GetItemQualityColor

local origs = {}
local tips = {GameTooltip, ItemRefTooltip, ShoppingTooltip1, ShoppingTooltip2}


--	Za Warudo!

local OnTooltipSetItem = function(self, ...)
	local orig = origs[self]
	if(orig) then orig(self, ...) end

	local name, item = self:GetItem()
	if(item) then
		local quality = select(3, GetItemInfo(item))
		if(quality) then
			local r, g, b = GetItemQualityColor(quality)

			self:SetBackdropBorderColor(r, g, b)
			self:SetBackdropColor(0, 0, 0, 1)
		end
	end
end

for _, obj in ipairs(tips) do
	origs[obj] = obj:GetScript"OnTooltipSetItem"
	obj:SetScript("OnTooltipSetItem", OnTooltipSetItem)
end]]
----------
--[[密语来电闪烁]]

local _G = getfenv(0)

_G.FCF_FlashTab = function()
	local this = this
	local tab = _G[this:GetName().."TabFlash"]

	if(not this.isDocked or (this == SELECTED_DOCK_FRAME) or UIFrameIsFlashing(tab)) then
		return
	end
	tab:Show()
	UIFrameFlash(tab, 0.25, 0.25, -1, nil, 0.5, 0.5)
end
----------
--[[飞行区域判断函数提供]]
-- IsFlyableArea() is kinda.. meh. lets change that.

local KRASUS_LANDING
local WINTERGRASP
local DALARAN

if (GetLocale() == "enGB" or GetLocale() == "enUS") then -- English
    KRASUS_LANDING = "Krasus' Landing"
    WINTERGRASP = "Wintergrasp"
    DALARAN = "Dalaran"
elseif (GetLocale() == "zhCN") then -- Chinese (simplified)
    KRASUS_LANDING = "克拉苏斯平台"
    WINTERGRASP = "冬拥湖"
    DALARAN = "达拉然"
elseif (GetLocale() == "zhTW") then -- Chinese (traditional)
    KRASUS_LANDING = "卡薩斯平臺"
    WINTERGRASP = "冬握湖"
    DALARAN = "達拉然"
elseif (GetLocale() == "ruRU") then -- Russian
    KRASUS_LANDING = ""
    WINTERGRASP = ""
    DALARAN = ""
end


function IsReallyFlyableArea()
    local flyable
    local mzt = GetMinimapZoneText()
    local rzt = GetRealZoneText()
    local t = {4}
    if (IsFlyableArea() and (rzt ~= DALARAN or mzt == KRASUS_LANDING)  and rzt ~= WINTERGRASP) then
        flyable = 1
    end
    return flyable
end


----------
--[[快速设置焦点 Focuser
local modifier = "shift" -- shift, alt or ctrl
local mouseButton = "1" -- 1 = left, 2 = right, 3 = middle, 4 and 5 = thumb buttons if there are any

local function SetFocusHotkey(frame)
	frame:SetAttribute(modifier.."-type"..mouseButton,"focus")
end

local function CreateFrame_Hook(type, name, parent, template)
	if name and template == "SecureUnitButtonTemplate" then
		SetFocusHotkey(_G[name])
	end
end

hooksecurefunc("CreateFrame", CreateFrame_Hook)

-- Keybinding override so that models can be shift/alt/ctrl+clicked
local f = CreateFrame("CheckButton", "FocuserButton", UIParent, "SecureActionButtonTemplate")
f:SetAttribute("type1","macro")
f:SetAttribute("macrotext","/focus mouseover")
SetOverrideBindingClick(FocuserButton,true,modifier.."-BUTTON"..mouseButton,"FocuserButton")

-- Set the keybindings on the default unit frames since we won't get any CreateFrame notification about them
local duf = {
	PlayerFrame,
	PetFrame,
	PartyMemberFrame1,
	PartyMemberFrame2,
	PartyMemberFrame3,
	PartyMemberFrame4,
	PartyMemberFrame1PetFrame,
	PartyMemberFrame2PetFrame,
	PartyMemberFrame3PetFrame,
	PartyMemberFrame4PetFrame,
	TargetFrame,
	TargetofTargetFrame,
}

for i,frame in pairs(duf) do
	SetFocusHotkey(frame)
end]]

----------
--[[Buttonrange 动作条距离染色]]
-- change the following values to define your out of range color (r, g, b)
local oor = { 0.8, 0, 0 };
-- don't change anything below
local Button_OnUpdate = function(self, elapsed)
	if ( self.rangeTimer == TOOLTIP_UPDATE_TIME ) then
		local range = false;
		if ( IsActionInRange(self.action) == 0 ) then
			getglobal(self:GetName().."Icon"):SetVertexColor(unpack(oor));
			getglobal(self:GetName().."NormalTexture"):SetVertexColor(unpack(oor));
			range = true;
		end;
		if ( self.range ~= range and range == false ) then
			ActionButton_UpdateUsable(self);
		end;
		self.range = range;
	end;
end;
hooksecurefunc("ActionButton_OnUpdate",Button_OnUpdate);

----------
--[[界面初始化]]
DW_SL_0 = "/console SET useUiScale 1\n/console SET UiScale 0.8\n/console SET scriptErrors 0\n/console SET profanityFilter 0 \n"
DW_SL_1 = "/script Bartender4.db:ResetProfile()\n/script Skada.db:ResetProfile()\n/script RangeDisplay.db:ResetProfile()\n"
--DW_SL_2 = "/script Quartz:ResetDB('profile') \n"

--[[
DW_SL_1 = "\n/sl {Frame = \"Violation0\", SetPoint = {\"BOTTOMRIGHT\", nil, \"BOTTOMRIGHT\", -91, 118}, SetMovable =true}"
DW_SL_2 = "\n/sl {Frame = \"MinimapButtonFrame\", SetPoint = {\"TOPRIGHT\", UIParent, \"TOPRIGHT\", -89, -268}, SetMovable =true}"
DW_SL_3 = "\n/sl {Frame = \"ZOMGBuffsButton\", SetPoint = {\"TOP\", nil, \"TOP\", -263, -118}, SetMovable =true}"
DW_SL_4 = "\n/sl {Frame = \"Steroids\", SetPoint = {\"TOPRIGHT\", nil, \"TOPRIGHT\", -173, -40}, SetMovable =true}"]]